Imports WindowsLive.Writer.Api
Imports System.Windows.Forms
Imports System.IO

<System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1705:LongAcronymsShouldBePascalCased")> <WriterPluginAttribute("CAEBBF30-0FD8-4033-BD6D-1F6AA1164E87", _
    "Text Template", _
    ImagePath:="text_signature.png", HasEditableOptions:=True, _
    PublisherUrl:="http://coolthingoftheday.blogspot.com/", _
    Description:="Insert and manage commonly used text templates")> _
<InsertableContentSourceAttribute("Text Template")> _
Public Class WLWTextTemplate
  Inherits ContentSource

  Private _Settings As Settings

  Public Sub New()

  End Sub

  Public Overrides Sub Initialize(ByVal pluginOptions As WindowsLive.Writer.Api.IProperties)
    MyBase.Initialize(pluginOptions)
    _Settings = New Settings(pluginOptions)
  End Sub

  Public Overrides Function CreateContent(ByVal dialogOwner As IWin32Window, ByRef newContent As String) As DialogResult
    Dim SelectTemplate As SelectTextTemplate
    Dim result As DialogResult

    SelectTemplate = New SelectTextTemplate
    result = SelectTemplate.ShowDialog(dialogOwner, _Settings)

    If result = DialogResult.OK Then

      If SelectTemplate.SelectedText.Length > 0 Then
        newContent = SelectTemplate.SelectedText
        SelectTemplate.Dispose()
        Return result

      Else
        SelectTemplate.Dispose()
        Return DialogResult.Cancel
      End If

    Else
      SelectTemplate.Dispose()
      Return result

    End If

  End Function

  Public Overrides Sub EditOptions(ByVal dialogOwner As System.Windows.Forms.IWin32Window)
    Dim EditOptions As Options
    Dim OriginalTemplatePath As String = _Settings.TemplatePath
    Dim NewTemplatePath As String = ""
    Dim returnValue() As String

    EditOptions = New Options
    EditOptions.TemplatePath = OriginalTemplatePath

    If EditOptions.ShowDialog(dialogOwner) = DialogResult.OK Then
      NewTemplatePath = EditOptions.TemplatePath

      If NewTemplatePath.Length > 0 Then
        _Settings.TemplatePath = NewTemplatePath
      End If

      If OriginalTemplatePath <> NewTemplatePath Then

        If Directory.Exists(NewTemplatePath) And IO.Directory.Exists(OriginalTemplatePath) Then
          returnValue = Directory.GetFiles(OriginalTemplatePath, "*.xml")

          If returnValue.Length > 0 Then

            If MsgBox("Would you like to copy the " & returnValue.Length & " XML files from " & OriginalTemplatePath & " to " & NewTemplatePath & " ?", MsgBoxStyle.YesNo, "Copy Files from the old Template Path to the new one?") = MsgBoxResult.Yes Then
              For Each Templatefile As String In returnValue
                Try
                  My.Computer.FileSystem.CopyFile(Templatefile, Path.Combine(NewTemplatePath, Path.GetFileName(Templatefile)), FileIO.UIOption.AllDialogs)

                Catch ex As Exception

                End Try

              Next
            End If

          End If 'If returnValue.Length > 0 Then

        End If 'If Directory.Exists(NewTemplatePath) And IO.Directory.Exists(OriginalTemplatePath) Then

      End If 'If OriginalTemplatePath <> NewTemplatePath Then

    End If 'If EditOptions.ShowDialog(dialogOwner) = DialogResult.OK Then

    EditOptions.Dispose()

  End Sub

End Class
